
var menuTreeSelectedItem = null;
var menuTreeStr = "<menu_tree>"
	+ "<item id='doc_list_docs_all' value='所有文件' class='doc_list_tree_menu_level1'"
	+ " image='../images/docs/alldocs.gif' click=''>" 
	+ "<item id='doc_list_docs_my' value='我的文件' class='doc_list_tree_menu_level2'"
	+ " image='../images/docs/icon_pencilandpad.gif' click=''></item>"
	+ "<item id='doc_list_docs_star' value='星号标识的文件' class='doc_list_tree_menu_level2'"
	+ " image='../images/docs/star_on_unselected.gif' click=''></item>"
	+ "<item id='doc_list_docs_recycle_bin' value='回收站' class='doc_list_tree_menu_level2'"
	+ " image='../images/docs/trash.gif' click=''></item>"
	+ "</item>"
	+ "<item id='doc_list_category_all' value='文件夹' class='doc_list_tree_menu_level1'"
	+ " image='../images/docs/icon_folders.gif' click=''>" 
	+ "<item id='doc_list_category_not_in' value='不在文件夹中的文件' class='doc_list_tree_menu_level2'"
	+ " image='../images/docs/icon_itemsnotinfolders.gif' click=''></item>"
	+ "</item>"
	+ "<item id='doc_list_category_custom' value='自定义' class='doc_list_tree_menu_level1'"
	+ " image='../images/docs/glass_large.gif' click=''>" 
	+ "</item>"
	+ "</menu_tree>";

/**
 * 从字符串中构造xml document
 * 并调用创建目录树函数
 **/
function buildMenuTree(menuTreeStr) {
	var xmlDoc = parseXML(menuTreeStr);
	
	var parentNode = $("doc_list_tree_menu");
	var levelFirstNodes = xmlDoc.selectNodes("//menu_tree/item");
	
	/*
	 * 调用创建目录树函数
	 */
	buildMenuTreeDiv(parentNode, levelFirstNodes);
	
	/*
	 * 展开第一个
	 */
	menuTreeShowFirst(parentNode, levelFirstNodes);
}

/**
 * 创建目录树
 **/
function buildMenuTreeDiv(parentNode, divNodes) {
	for(var i = 0; i < divNodes.length; i++) {
		var divNode = divNodes[i];
		var nextLevelItems = divNode.selectNodes("item");
		
		var itemDiv = document.createElement("div");
		itemDiv.setAttribute("id", divNode.getAttribute("id"));
		Element.setStyle(itemDiv, {cursor: "pointer"});
		itemDiv.className = divNode.getAttribute("class");
		Event.observe(itemDiv, "click", menuTreeSelectItemStyleHandle.bindAsEventListener(this, itemDiv), false);
		var itemImg1 = document.createElement("img");
		if(nextLevelItems.length != 0) {
			itemImg1.setAttribute("src", "../images/docs/button_plus_small.gif");
			Event.observe(itemImg1, "click", menuTreeCollapseAndExpandHandle.bindAsEventListener(this, itemImg1, divNode.getAttribute("id") + "_container"), false);
		} else {
			itemImg1.setAttribute("src", "../images/docs/blank.gif");
			itemImg1.setAttribute("width", "10");
			itemImg1.setAttribute("height", "10");
		}
		var itemImg2 = document.createElement("img");
		itemImg2.className = "doc_list_tree_menu_img";
		itemImg2.setAttribute("src", divNode.getAttribute("image"));
		itemDiv.appendChild(itemImg1);
		itemDiv.appendChild(document.createTextNode(" "));
		itemDiv.appendChild(itemImg2);
		itemDiv.appendChild(document.createTextNode(" "));
		itemDiv.appendChild(document.createTextNode(divNode.getAttribute("value")));
		
		parentNode.appendChild(itemDiv);
		
		if(nextLevelItems.length != 0) {
			var nextLevelParentNode = document.createElement("div");
			nextLevelParentNode.setAttribute("id", divNode.getAttribute("id") + "_container");
			Element.setStyle(nextLevelParentNode, {display:"none"});
			parentNode.appendChild(nextLevelParentNode);
			buildMenuTreeDiv(nextLevelParentNode, nextLevelItems);
		}
	}
}

/**
 * 目录树的展开和收缩 handle
 **/
function menuTreeCollapseAndExpandHandle(event, imgItem, divId) {
	menuTreeCollapseAndExpand(imgItem, divId);
}

/**
 * 目录树的展开和收缩
 **/
function menuTreeCollapseAndExpand(imgItem, divId) {
	var targetDiv = $(divId);
	if(targetDiv.style.display == "none") {
		targetDiv.style.display = "";
		imgItem.setAttribute("src", "../images/docs/button_minus_small.gif");
	} else {
		targetDiv.style.display = "none";
		imgItem.setAttribute("src", "../images/docs/button_plus_small.gif");
	}
}

/**
 * 默认把目录树的第一项展开
 **/
function menuTreeShowFirst(parentNode, nodes) {
	if(nodes.length > 0) {
		var firstNode = nodes[0];
		if(firstNode.selectNodes("item").length > 0) {
			var imgs = parentNode.getElementsByTagName("img");
			var targetImg;
			if(imgs.length > 0) {
				targetImg = imgs[0];
			} else {
				return;
			}
			var divId = firstNode.getAttribute("id");
			menuTreeCollapseAndExpand(targetImg, divId + "_container");
			
			var divItem = $(divId);
			menuTreeSelectItemStyle(divItem);
		}
	}
}

/**
 * 目录树的项被选中的样式 handle
 **/
function menuTreeSelectItemStyleHandle(event, selectedItem) {
	menuTreeSelectItemStyle(selectedItem);
}

/**
 * 目录树的项被选中后的样式
 * 原来的选中要变会没有选中的样式
 **/
function menuTreeSelectItemStyle(selectedItem) {
	if(menuTreeSelectedItem != null) {
		menuTreeSelectedItem.style.background = "#FFF";
	}
	selectedItem.style.background = "#EEE";
	menuTreeSelectedItem = selectedItem;
}